from concurrent.futures import ThreadPoolExecutor
from time import sleep


# def func(name):
#     for i in range(10):
#         print(name,i)

# if __name__ == '__main__':
#     with ThreadPoolExecutor(10) as t:  # 线程池里给10个线程
#         t.submit(func, '陈奕迅')
#         t.submit(func, '王菲')
#         t.submit(func, '杨幂')
#
#         for i in range(100):
#             t.submit(func,f'周杰伦{i}')  # 100个任务给线程池里的10个线程去跑



def func1(name,time_sleep):
    sleep(time_sleep)
    print("我是",name)
    return name

def fn(res):
    print(res.result())

if __name__ == '__main__':
    with ThreadPoolExecutor(3) as t:
        # 获取返回值方法1
        t.submit(func1, '陈奕迅',2).add_done_callback(fn) # add_done_callback() 函数可获取return 返回值
        t.submit(func1, '王菲',1).add_done_callback(fn)
        t.submit(func1, '杨幂',3).add_done_callback(fn)

        '''
        t.submit().add_done_callback() 返回即执行 callback函数
        返回callback 函数执行的顺序是不确定的，返回值的顺序是不确定的
        '''

        # 获取返回值方法2
        # map() 映射也可获取return返回值 传参对象需可迭代
        result = t.map(func1,['陈奕迅','王菲','杨幂'],[2,1,3])
        for r in result:
            print(r)
        # map() 返回值是生成器，返回的内容和任务分发的是一致的






